//
//  LSYPhotoAddView.m
//  TTClub
//
//  Created by xkun on 15/9/25.
//  Copyright (c) 2015年 TTClub. All rights reserved.
//

#define kSpace                      0    //图片间距 上下左右
#define nMaxNumOneLine              4     //一行最多可显示图片个数
#define BaseTag                     100

#import "LSYPhotoAddView.h"
#import "LSYEditorImageView.h"
#import <Masonry.h>
@interface LSYPhotoAddView ()

@property (strong, nonatomic) UIButton *addButton;

@end

@implementation LSYPhotoAddView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setUPUI];

    }
    return self;
}

- (void)awakeFromNib{
    [self setUPUI];

}
- (void)setUPUI
{
    _imageAligment = LSYImageAlignmentLeft;
    _imageUrlArray = [[NSMutableArray alloc]init];
    [self addSubview:self.addButton];
    [self choseAddButtonFrame];
}

+ (CGFloat)getLSYPhotoAddViewHeightByImageCount:(NSInteger)count
{
    count += 1;    //至少一张图 默认添加图
    CGFloat totalHeight = 0;
    NSInteger lines =  ((count - 1)/nMaxNumOneLine + 1);
    CGFloat imageHeigh = [[self class] getImageHeight];
    totalHeight += (lines - 1) * kSpace;
    totalHeight += kSpace * 2;
    totalHeight += lines *imageHeigh;
    return totalHeight;
}

#pragma mark - private

- (void)choseAddButtonFrame
{
    WS(weakSelf);
    CGFloat width = [[weakSelf class] getImageWidth];
    if (_imageAligment == LSYImageAlignmentLeft) {
        [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(kSpace);
            make.bottom.mas_equalTo(-kSpace);
            make.width.mas_equalTo(width);
        }];
    } else if (_imageAligment == LSYImageAlignmentCenter) {
        [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(weakSelf);
            make.top.mas_equalTo(kSpace);
            make.bottom.mas_equalTo(-kSpace);
            make.width.mas_equalTo(width);
        }];
    }
    
}

- (void)addNewImageAction
{
    if (_addButtonClickBlock) {
        _addButtonClickBlock();
    }
}

- (void)delCurrentImage:(UITapGestureRecognizer *)tapGesture
{
    UIView *slectedView = tapGesture.view;
    if (_delButtonClickBlock) {
        NSInteger index = (slectedView.tag - BaseTag);
        _delButtonClickBlock(index);
    }
}

- (void)resetPhotoView
{
    for (UIView *oneView in [self subviews]) {
        self.addButton = nil;
        [oneView removeFromSuperview];
    }
    CGFloat imageWidth =  [[self class]getImageWidth];
    if (_imageUrlArray.count > 0) {
        for (int i = 0; i < _imageUrlArray.count + 1; i++) {
            NSInteger xSize = i % nMaxNumOneLine;
            NSInteger ySize = i/nMaxNumOneLine;
            if (i < _imageUrlArray.count) {
                LSYEditorImageView *oneImageView = [[LSYEditorImageView alloc]init];
                oneImageView.tag = (BaseTag + i);
                UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(delCurrentImage:)];
                [oneImageView addGestureRecognizer:tapGesture];
                
                oneImageView.frame = CGRectMake(0 + xSize * (kSpace + imageWidth), kSpace + ySize * ([[self class]getImageHeight] + kSpace),[[self class]getImageWidth], [[self class]getImageHeight]);
                [self addSubview:oneImageView];
                [oneImageView.showImageView setImage:_imageUrlArray[i]];
            } else {
                [self addSubview:self.addButton];
                self.addButton.frame = CGRectMake(0 + xSize * (kSpace + imageWidth), kSpace + ySize * ([[self class]getImageHeight] + kSpace),[[self class]getImageWidth], [[self class]getImageHeight]);
            }
            
        }
    } else {
        [self addSubview:self.addButton];
        self.addButton.frame = CGRectMake(0, kSpace,[[self class]getImageWidth], [[self class]getImageHeight]);
    }
}

+ (CGFloat)getImageWidth
{
    return ((LYScreenWidth - 26) - (nMaxNumOneLine - 1) * kSpace)/nMaxNumOneLine * 1.0;
}
+ (CGFloat)getImageHeight
{
    return [self getImageWidth];
}
#pragma mark - setter

- (void)setImageUrlArray:(NSMutableArray *)imageUrlArray
{
    _imageUrlArray = imageUrlArray;
}
- (void)setImageAligment:(LSYImageAlignment)imageAligment
{
    if (_imageAligment == imageAligment) {
        return;
    }
    _imageAligment = imageAligment;
    [self choseAddButtonFrame];
}
#pragma mark - getter

- (UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addButton setImage:[UIImage imageNamed:@"添加图片"] forState:UIControlStateNormal];
        [_addButton setContentEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
        [_addButton addTarget:self action:@selector(addNewImageAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

@end
